/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GestureRecognition;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.logging.Level;

/**
 *
 * @author Home
 */
public class Slide {

    static int frameCount = 0;
    static int samePoint = 0;
    final int slide_scroll_min = 200;
    private static int[] prevPoint = new int[2];
    final int MAX_SAME_POINT_X = 100;
    final int MIN_ROTATE = 10;
    final int MAX_SLIDE = 400;
//    final int MIN_SLIDE_Y = 100;
    //  final int MAX_SCROLL = 5;
    final int MAX_FRAME_COUNT = 10;
    int dx1 = 0, dx2 = 0, dy1 = 0, dy2 = 0, d1 = 0, d2 = 0;
    int[] dx = new int[MAX_FRAME_COUNT];
    int[] dy = new int[MAX_FRAME_COUNT];

    public Slide() {

        for (int i = 0; i < MAX_FRAME_COUNT - 2; i++) {
            dx[i] = 0;
            dy[i] = 0;
        }
    }

    // 0: No Gesture
    //+1: Slide Right
    //-1: Slide Left
    //+2: Scroll Up
    //-2: Scroll Down
    public int identifyGestureType(int[] currentPoint) {
        int ret = 0;
        double rx = 1;
        double ry = 0;

//        if (frameCount < MAX_FRAME_COUNT) {
//            frameCount++;
//        } else {
//            frameCount = frameCount % MAX_FRAME_COUNT;
//        }

        dx1 = 0;
        dy1 = 0;
        for (int i = 0; i < MAX_FRAME_COUNT - 1; i++) {

            dx[i] = dx[i + 1];
            dy[i] = dy[i + 1];
            dx1 = dx1 + dx[i];
            dy1 = dy1 + dy[i];
            rx = rx + Math.copySign(1, dx[i]);
            ry = ry + Math.copySign(1, dy[i]);

            // System.out.println("in loop"+i+" dx1="+dx1+", dy1="+dy1);
        }

        d1 = currentPoint[0] - prevPoint[0];
        d2 = currentPoint[1] - prevPoint[1];

//        if (Math.abs(ry) < MAX_FRAME_COUNT - 2 || Math.abs(rx) < MAX_FRAME_COUNT - 2) {
//            System.out.println("dir");
//            dx1 = 0;
//            dy1 = 0;
//            dx = new int[MAX_FRAME_COUNT];
//            dy = new int[MAX_FRAME_COUNT];
//            return 0;
//
//
//        }
//        if(d1>=MAX_SLIDE-100 || d2>=MAX_SLIDE-100){
//            d1=0;
//            d2=0;
//            dx= new int[MAX_FRAME_COUNT];
//            dy= new int[MAX_FRAME_COUNT];
//            dx1=0;
//            dx2=0;
//            return 0;
//        }

        dx[MAX_FRAME_COUNT - 1] = d1;
        dy[MAX_FRAME_COUNT - 1] = d2;

//        if (d1 < MAX_SLIDE - 100 || d2 > MAX_SLIDE - 100) {
//            d1 = 0;
//            d2 = 0;
//            dx = new int[MAX_FRAME_COUNT];
//            dy = new int[MAX_FRAME_COUNT];
//            dx1 = 0;
//            dy1 = 0;
//            return 0;
//        }


//        if (d1 < MAX_SAME_POINT_X && d2 < MAX_SAME_POINT_X) {
//            samePoint++;
//            if (samePoint == 40) {
//                samePoint = 0;
//                dx1 = dy1 = 0;
//                d1 = d2 = 0;
//                //  System.out.println("same point");
//            }
//        } else {
//            samePoint = 0;
//        }
        // System.out.println("dx1="+dx1+",dy1="+dy1);

        dx1 = dx1 + d1;
        dy1 = dy1 + d2;
        prevPoint[0] = currentPoint[0];
        prevPoint[1] = currentPoint[1];

        //System.out.println("dx1="+dx1+",dy1="+dy1);

        if ((Math.abs(dx1) > MAX_SLIDE) && (Math.abs(dy1) < slide_scroll_min)) {
            System.out.println("dx1=" + dx1 + " dy1=" + dy1);
            int dir = 1;
            if (dx1 > 0) {
                System.out.println("slide right");
                ret = +1;
                slide(ret);
            } else {
                System.out.println("slide left");
                ret = -1;
                slide(ret);
            }
            dx = new int[MAX_FRAME_COUNT];
            dy = new int[MAX_FRAME_COUNT];

            dx1 = 0;
            dy1 = 0;
            //    slide(ret);
            return ret;
        } else if ((Math.abs(dx1) < slide_scroll_min) && (Math.abs(dy1) > MAX_SLIDE)) {
            int dir = 1;
            System.out.println("dx1=" + dx1 + " dy1=" + dy1);
            if (dy1 < 0) {
                System.out.println("scroll up");
                ret = -2;
                slide(ret);
            } else {
                System.out.println("scroll down");
                ret = +2;
                slide(ret);
            }
            dx = new int[MAX_FRAME_COUNT];
            dy = new int[MAX_FRAME_COUNT];
            dx1 = 0;
            dy1 = 0;
            //   slide(ret);
            return ret;
        } else {
            return 0;
        }

    }

    public void rotate(int dir, double angle) {
        System.out.println("Rotate");
        System.out.println("Direction= " + dir);
        System.out.println("Angle= " + angle);
    }

    public void slide(int direction) {
        try {
            Robot rb = new Robot();
            switch (direction) {
                case -1:
                    rb.keyPress(KeyEvent.VK_LEFT);
                    rb.keyRelease(KeyEvent.VK_LEFT);
                    break;
                case +1:
                    rb.keyPress(KeyEvent.VK_RIGHT);
                    rb.keyRelease(KeyEvent.VK_RIGHT);
                    break;
                case -2:
                    rb.keyPress(KeyEvent.VK_UP);
                    rb.keyRelease(KeyEvent.VK_UP);
                    break;
                case +2:
                    rb.keyPress(KeyEvent.VK_DOWN);
                    rb.keyRelease(KeyEvent.VK_DOWN);
                    break;
            }
        } catch (AWTException ex) {
            java.util.logging.Logger.getLogger(Slide.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void scroll(int distance) {
        try {
            //distance=# of wheels
            Robot rb = new Robot();
            rb.mouseWheel(distance);
        } catch (AWTException ex) {
            java.util.logging.Logger.getLogger(Slide.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void zooming(int factor) {
        try {
            Robot rb = new Robot();
            if (factor > 0) {
                //Zoom in
                rb.keyPress(KeyEvent.VK_CONTROL);
                rb.keyPress(KeyEvent.VK_SHIFT);
                rb.keyPress(KeyEvent.VK_EQUALS);
                rb.keyRelease(KeyEvent.VK_CONTROL);
                rb.keyRelease(KeyEvent.VK_SHIFT);
                rb.keyRelease(KeyEvent.VK_EQUALS);
            } else {
                //Zoom out
                rb.keyPress(KeyEvent.VK_CONTROL);
                rb.keyPress(KeyEvent.VK_SHIFT);
                rb.keyPress(KeyEvent.VK_MINUS);
                rb.keyRelease(KeyEvent.VK_CONTROL);
                rb.keyRelease(KeyEvent.VK_SHIFT);
                rb.keyRelease(KeyEvent.VK_MINUS);
            }
        } catch (AWTException ex) {
            java.util.logging.Logger.getLogger(Slide.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public static void main(String[] args) {

//        int[] lc = new int[2];
//        Slide sl = new Slide();
//        for (int hj = 0; hj < 30; hj++) {
//            lc[1] = hj * 150;
//            System.out.println(hj + " Distance=" + hj * 150 + " gesture=" + sl.identifyGestureType(lc));
//        }
//        for (int i = 0; i <100; i++) {
//
//            if(i<41){
//                lc[0]=1;
//                lc[1]=i;
//            }
//            else if(41<=i&&51<i){
//                lc[0]=41-60*(i-41);
//                lc[1]=i;
//            }
//
//            //System.out.println(i);
//            sl.identifyGestureType(lc);
//        }

        for (int i = 0; i < 1000; i++) {

            new Slide().slide(1);
        }

    }
}
